package com.kateba.android.bluetooth;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;

public class BluetoothReflection
{
  public static int TYPE_L2CAP = 3;

  private static void bindListen(BluetoothServerSocket paramBluetoothServerSocket)
    throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException
  {
    BluetoothSocket localBluetoothSocket = getSocket(paramBluetoothServerSocket);
    Method localMethod = BluetoothSocket.class.getDeclaredMethod("bindListen", new Class[0]);
    localMethod.setAccessible(true);
    int i = ((Integer)localMethod.invoke(localBluetoothSocket, new Object[0])).intValue();
    if (i != 0);
    try
    {
      paramBluetoothServerSocket.close();
      //label46:
    	  throwErrnoNative(localBluetoothSocket, i);
      return;
    }
    catch (IOException localIOException)
    {
   //   break label46;
    }
  }

  private static BluetoothServerSocket createBluetoothServerSocket(int paramInt1, boolean paramBoolean1, boolean paramBoolean2, int paramInt2)
  {
    BluetoothServerSocket localBluetoothServerSocket;
	try
    {
      Class[] arrayOfClass = new Class[4];
      arrayOfClass[0] = Integer.TYPE;
      arrayOfClass[1] = Boolean.TYPE;
      arrayOfClass[2] = Boolean.TYPE;
      arrayOfClass[3] = Integer.TYPE;
      Constructor localConstructor = BluetoothServerSocket.class.getDeclaredConstructor(arrayOfClass);
      localConstructor.setAccessible(true);
      Object[] arrayOfObject = new Object[4];
      arrayOfObject[0] = Integer.valueOf(paramInt1);
      arrayOfObject[1] = Boolean.valueOf(paramBoolean1);
      arrayOfObject[2] = Boolean.valueOf(paramBoolean2);
      arrayOfObject[3] = Integer.valueOf(paramInt2);
      localBluetoothServerSocket = (BluetoothServerSocket)localConstructor.newInstance(arrayOfObject);
      try
      {
        bindListen(localBluetoothServerSocket);
        return localBluetoothServerSocket;
      }
      catch (Exception localException2)
      {
        while (true)
          localBluetoothServerSocket = null;
      }
    }
    catch (Exception localException1)
    {
      while (true)
         localBluetoothServerSocket = null;
    }
  }

  private static BluetoothSocket createBluetoothSocket(int paramInt1, int paramInt2, boolean paramBoolean1, boolean paramBoolean2, String paramString, int paramInt3)
  {
    BluetoothSocket localBluetoothSocket;
	try
    {
      Class[] arrayOfClass = new Class[6];
      arrayOfClass[0] = Integer.TYPE;
      arrayOfClass[1] = Integer.TYPE;
      arrayOfClass[2] = Boolean.TYPE;
      arrayOfClass[3] = Boolean.TYPE;
      arrayOfClass[4] = String.class;
      arrayOfClass[5] = Integer.TYPE;
      Constructor localConstructor = BluetoothSocket.class.getDeclaredConstructor(arrayOfClass);
      localConstructor.setAccessible(true);
      Object[] arrayOfObject = new Object[6];
      arrayOfObject[0] = Integer.valueOf(paramInt1);
      arrayOfObject[1] = Integer.valueOf(paramInt2);
      arrayOfObject[2] = Boolean.valueOf(paramBoolean1);
      arrayOfObject[3] = Boolean.valueOf(paramBoolean2);
      arrayOfObject[4] = paramString;
      arrayOfObject[5] = Integer.valueOf(paramInt3);
      localBluetoothSocket = (BluetoothSocket)localConstructor.newInstance(arrayOfObject);
      return localBluetoothSocket;
    }
    catch (Exception localException)
    {
      while (true)
         localBluetoothSocket = null;
    }
  }

  public static BluetoothServerSocket createL2CAPBluetoothServerSocket(int paramInt)
  {
    return createBluetoothServerSocket(TYPE_L2CAP, false, false, paramInt);
  }

  public static BluetoothSocket createL2CAPBluetoothSocket(String paramString, int paramInt)
  {
    return createBluetoothSocket(TYPE_L2CAP, -1, false, false, paramString, paramInt);
  }

  private static BluetoothSocket getSocket(BluetoothServerSocket paramBluetoothServerSocket)
    throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
  {
    Field localField = BluetoothServerSocket.class.getDeclaredField("mSocket");
    localField.setAccessible(true);
    return (BluetoothSocket)localField.get(paramBluetoothServerSocket);
  }

  private static void throwErrnoNative(BluetoothSocket paramBluetoothSocket, int paramInt)
    throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
  {
    Class[] arrayOfClass = new Class[1];
    arrayOfClass[0] = Integer.TYPE;
    Method localMethod = BluetoothSocket.class.getDeclaredMethod("throwErrnoNative", arrayOfClass);
    localMethod.setAccessible(true);
    Object[] arrayOfObject = new Object[1];
    arrayOfObject[0] = Integer.valueOf(paramInt);
    localMethod.invoke(paramBluetoothSocket, arrayOfObject);
  }
}